package com.xinshujia.config;

import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.context.annotation.Configuration;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-25 15:33       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 */
@Configuration
public class MqttCallBack implements MqttCallback {
    /**
     * 与服务器断开的回调
     */
    @Override
    public void connectionLost(Throwable cause) {
        System.out.println("与服务器断开连接");
    }

    /**
     * 消息到达的回调
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) {
        System.out.println("【发送】消息主题: " + topic);
        System.out.println("【发送】消息Qos: " + message.getQos());
        System.out.println("【发送】消息内容: " + new String(message.getPayload()));
        System.out.println("【发送】消息retained: " + message.isRetained());
    }

    /**
     * 消息发布成功的回调
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        IMqttAsyncClient client = token.getClient();
        System.out.println("【发送】" + client.getClientId() + "##########发布消息成功！");
    }
}